using System;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Modules.InnerCity
{
    public class TileCollideManager : MonoBehaviour
    {

        public Vector2 m_size = new Vector2(0.4f, 0.4f);
        
        private bool m_is_need_update_onece;
        
        private RotateRect m_rect = new RotateRect(Vector2.zero, Vector2.one);

        private List<TileCollideItem> tileCollision = new List<TileCollideItem>();

        private List<TileCollideItem> list = new List<TileCollideItem>(200);
        private List<TileCollideItem> activeList = new List<TileCollideItem>(200);

        [DisplayOnly]
        public float BuildingDefaultScale = 1f;
        
        private float _lastLinearValue = -1;
        private float _lastScale = -1;


        public void OnUpdate()
        {
            if (m_is_need_update_onece)
            {
                UpdateLastScale();
                m_is_need_update_onece = false;
            }
        }


        public void UpdateLastScale()
        {
            this.tileCollision.Sort((x, y) => y.priority - x.priority);
            
            if (_lastScale > 0)
            {
                SetScale(_lastLinearValue,_lastScale,true);
            }
            else
            {
                SetScale(1,BuildingDefaultScale,true);
            }
        }

        

        private void SetCollideItemScale(TileCollideItem tileCollideItem,float scale)
        {
            tileCollideItem.SetScale(BuildingDefaultScale);
        }


        public void OnExitScale(float linerValue,float scale)
        {
            list.Clear();

            for (int i = 0; i < tileCollision.Count; i++)
            {
                var tileCollideItem2 = tileCollision[i];
                SetCollideItemScale(tileCollideItem2,scale);
                tileCollideItem2.ShiftPos(linerValue);
                
                if (!tileCollideItem2.is_active)
                {
                    list.Add(tileCollideItem2);
                }
            }

            foreach (var collideItem in list)
            {
                collideItem.Activate();
            }
            
            _lastLinearValue = linerValue;
            _lastScale = scale;

        }

        public void SetScale(float linerValue,float scale,bool force = false)
        {
            list.Clear();
            activeList.Clear();
            scale = BuildingDefaultScale;
            for (int i = 0; i < tileCollision.Count; i++)
            {
                var tileCollideItem2 = tileCollision[i];
                SetCollideItemScale(tileCollideItem2,scale);
                tileCollideItem2.ShiftPos(linerValue);
            }


            if (force)
            {
                for (int i = 0; i < tileCollision.Count; i++)
                {
                    var tileCollideItem = tileCollision[i];
                    list.Add(tileCollideItem);
                    
                    for (int j = 0; j <= list.Count - 1; j++)
                    {
                        for (int k = j + 1; k < list.Count; k++)
                        {
                            if (list[j].GetGlobalRect().Overlaps(list[k].GetGlobalRect()))
                            {
                                list[k].Deactivate();
                                list.RemoveAt(k);
                                k--;
                            }
                        }
                    }
                }
            }
            else if (linerValue > _lastLinearValue) 
            {
                // 抬高

                for (int i = 0; i < tileCollision.Count; i++)
                {
                    var tileCollideItem = tileCollision[i];
                    if (tileCollideItem.is_active)
                    {
                        list.Add(tileCollideItem);
                    }
                }
                
                            
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    for (int k = j + 1; k < list.Count; k++)
                    {
                        if (list[j].GetGlobalRect().Overlaps(list[k].GetGlobalRect()))
                        {
                            list[k].Deactivate();
                            list.RemoveAt(k);
                            k--;
                        }
                    }
                }


            }
            else
            {
                // 降低
                for (int i = 0; i < tileCollision.Count; i++)
                {
                    var tileCollideItem = tileCollision[i];
                    
                    if (!tileCollideItem.is_active)
                    {
                        list.Add(tileCollideItem);
                    }
                    else
                    {
                        activeList.Add(tileCollideItem);
                    }
                    
                }
                
                
                for (int num = 0; num != list.Count; num++)
                {
                    bool tmp = false;
                    for (int i = 0; i < activeList.Count; i++)
                    {
                        if (list[num].GetGlobalRect().Overlaps(activeList[i].GetGlobalRect()))
                        {
                            tmp = true;
                            break;
                        }
                    }

                    if (!tmp)
                    {
                        list[num].Activate();
                    }
                }
                
            }
            

            _lastLinearValue = linerValue;
            _lastScale = scale;
        }
        
        
        private RotateRect GetGlobalRect()
        {
            Vector3 position = base.transform.position;
            this.m_rect.UpdateRect(new Vector2(base.transform.position.x, base.transform.position.z),
                new Vector2(this.m_size.x, this.m_size.y));
            return this.m_rect;
        }
        


        public void Add(TileCollideItem tc)
        {
            if (tc != null && !this.tileCollision.Contains(tc))
            {
                tc.Activate();
                tc.is_active = true;
                //SetCollideItemScale(tc, _lastScale);
                tc.transform.localScale = Vector3.zero;
                m_is_need_update_onece = true;
                tc.SetTileCollideManager(this);
                this.tileCollision.Add(tc);
            }
        }

        public void Remove(TileCollideItem tc)
        {
            if (tc != null)
            {
                this.tileCollision.Remove(tc);
            }
        }

        public void RemoveAll()
        {
            this.tileCollision.Clear();
        }

        public void ResetToPool()
        {
            RemoveAll();
            _lastLinearValue = -1f;
            _lastScale = -1f;
        }
        
        
        
        private void DebugDrawRect()
        {
            RotateRect globalRect = this.GetGlobalRect();
            Vector3 vector = new Vector3(globalRect.world_corners[0].x, 0f, globalRect.world_corners[0].y);
            Vector3 vector2 = new Vector3(globalRect.world_corners[1].x, 0f, globalRect.world_corners[1].y);
            Vector3 vector3 = new Vector3(globalRect.world_corners[2].x, 0f, globalRect.world_corners[2].y);
            Vector3 vector4 = new Vector3(globalRect.world_corners[3].x, 0f, globalRect.world_corners[3].y);
            Gizmos.DrawLine(vector, vector2);
            Gizmos.DrawLine(vector2, vector3);
            Gizmos.DrawLine(vector3, vector4);
            Gizmos.DrawLine(vector4, vector);
        }

        public void OnDrawGizmos()
        {
            Gizmos.color = Color.green;
            this.DebugDrawRect();
        }

        public void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.red;
            this.DebugDrawRect();
        }

        
    }
}